package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.crm.entity.CrmClueActivityRelation;
import com.crm.mapper.CrmClueActivityRelationMapper;
import com.crm.service.ICrmClueActivityRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.util.ObjectValidate;
import com.crm.util.UUIDUtils;
import com.crm.vo.ClueActivityRelation;
import com.crm.vo.SysActivity;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author junxian,dou
 * @since 2024-11-25
 */
@Service
public class CrmClueActivityRelationServiceImpl extends ServiceImpl<CrmClueActivityRelationMapper, CrmClueActivityRelation> implements ICrmClueActivityRelationService {

    @Override
    public void addClueActivityRelation(ClueActivityRelation clueActivityRelation) {
        ObjectValidate.ValidateIsEmpty(clueActivityRelation);
        List<String> activityIds = clueActivityRelation.getActivityIds();
        String clueId = clueActivityRelation.getClueId();
        if (!activityIds.isEmpty() && ObjectUtil.isNotEmpty(clueId)){
            List<CrmClueActivityRelation> relationList = activityIds.stream().map(activityId -> {
                CrmClueActivityRelation relation = new CrmClueActivityRelation();
                relation.setId(UUIDUtils.getUUID());
                relation.setActivityId(activityId);
                relation.setClueId(clueId);
                return relation;
            }).collect(Collectors.toList());
            saveBatch(relationList);
        }
    }

    @Override
    public List<SysActivity> getRelationActivityWithClueId(String id) {
       return this.baseMapper.getRelationActivityWithClueId(id,null);
    }

    @Override
    public List<SysActivity> queryActivityForConvertByNameClueId(String clueId,String name) {
        return this.baseMapper.getRelationActivityWithClueId(clueId,name);
    }

    @Override
    public void deleteClueActivityRelationWithId(String id) {

        ObjectValidate.ValidateIsEmpty(id);
        removeById(id);
    }
}
